/**
 * 
 */
package com.worksmap.core.listener;

import net.sf.json.JSONObject;

import com.taobao.api.internal.tmc.Message;
import com.worksmap.core.entity.Trade;

/**
 * @author jack
 *
 */
public abstract class TmcClientHelper {
	
	/**
	 * 交易评价变更消息:{"buyer_nick":"sandbox_cilai_c","payment":"105.00","oid":192329924021084,
	 * 			"tid":192329924021084,"type":"guarantee_trade","seller_nick":"sandbox_c_5"}
	 * jack
	 * 2013-12-31
	 */
	protected Trade getRatedTradeByMessage(Message message){
		return getTrade(message);
	}

	/**
	 * 获取交易成功订单
	 * 交易成功消息:{"buyer_nick":"sandbox_cilai_c","payment":"105.00","oid":192329924011084,
	       "tid":192329924011084,"type":"guarantee_trade","seller_nick":"sandbox_c_5"} 
	 */
	protected Trade getTradeByMessage(Message message){
		return getTrade(message);
	}
	
	private Trade getTrade(Message message){
		Trade trade = new Trade();
		JSONObject jsonObject = JSONObject.fromObject(message.getContent());
		trade.setOid(jsonObject.getString("oid"));
		trade.setSeller_nick(jsonObject.getString("seller_nick"));
		trade.setBuyer_nick(jsonObject.getString("buyer_nick"));
		trade.setTid(jsonObject.getString("tid"));
		return trade;
	}
	
}
